<template>
  <view class="main">
    <image class="logo" src="../../static/blogo.png" mode="scaleToFill" />
    <u-button v-if="!checkboxValue" type="primary" shape="circle" color="#38b5f8" text="快速登录" @click="loginClick"></u-button>
    <u-button v-else open-type="getPhoneNumber" shape="circle" @click="clickfn" color="#38b5f8" type="primary" @getphonenumber="getPhone" text="快速登录"></u-button>

    <u-checkbox-group v-model="checkboxValue" active-color="#38b5f8" shape="circle" placement="column">
      <view class="radioBox">
        <u-checkbox label="请阅读 同意" name="1"> </u-checkbox>
        <text class="xfxy" @click="goPage('/pages/myPage/userAgreement')">《用户协议》</text>
        <text class="xfxy" @click="goPage('/pages/myPage/privacyAgreement')">《隐私协议》</text>
      </view>
    </u-checkbox-group>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { bindMobileByWxCode, getUserInfo } from "@/api/my";
import { goPage } from "@/utils/commonFn";
const checkboxValue = ref();
const loginClick = () => {
  console.log(checkboxValue.value);
  uni.showToast({
    icon: "none",
    title: "请勾选隐私协议",
  });
};
const clickfn = () => {
  // 关注申请审核通知
  // 新的评论通知
  // 关注申请通知
  uni.requestSubscribeMessage({
    tmplIds: ["zDgQbqtBBuBwGbEmtAYBiPWE8vdKMW1ILc_vIgSjCdk", "OgQrcRQNcRxP74d0tBenhZI2u-73A5wo4mJktFb4mHQ", "UsGcL0kUCbAzqbJd_mbf7lYq-M9KstkGNDWxR6FPe84"],
    success() {},
  });
};
const getPhone = async (resPhone: any) => {
  const provider: any = await new Promise((resolve, reject) => {
    uni.getProvider({
      service: "oauth",
      success: function (res) {
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      },
    });
  });

  // 当前只做微信小程序
  if (!~provider.provider.indexOf("weixin")) return;
  const loginRes: any = await new Promise((resolve, reject) => {
    uni.login({
      provider: "weixin",
      success: function (res) {
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      },
    });
  });
  // 前端获取到微信登录临时码，传至后台换openId和sessionId
  let loginCode = loginRes.code;
  console.log(uni.getStorageSync("invitationCode"), "login");
  const {
    data: { token },
  } = await bindMobileByWxCode({ loginCode, code: resPhone.detail.code, invitationCode: uni.getStorageSync("invitationCode") || "" });

  uni.showToast({
    title: "登录成功",
  });
  uni.navigateBack();
  uni.setStorageSync("token", token);
  const res = await getUserInfo();
  uni.setStorageSync("userInfo", res.data);
};
</script>

<style lang="less" scoped>
.logo {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 80rpx;
}
.main {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.radioBox {
  display: flex;
  align-items: center;
  margin-top: 20rpx;
  .xfxy {
    font-size: 30rpx;
    color: #38b5f8;
  }
}
</style>
